<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.text.SimpleDateFormat,java.util.Date,java.text.ParseException,org.apache.commons.lang.*"%>
<%@page import="org.jawa.core.util.JawaGlobals"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

<%!
    static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd kk:mm:ss");

	private static String parseDate(String input) {
		if (input == null || "".equals(input)) {
			return input;
		}
		if (input.length() < 19) {
			return input;
		}
		String d = input.substring(0, 19);
		// try to parse it
		try {
			StringBuffer buf = new StringBuffer(input.length());
			synchronized (formatter) {
				Date date = formatter.parse(d);
				buf.append("<span class=\"date\" title=\"").append(formatter.format(date)).append("\">");
			}
			buf.append(d).append("</span>");
			buf.append(input.substring(19, input.length()));
			return buf.toString();
		} catch (ParseException pe) {
			return input;
		}
	}

	private static String hilite(String input) {
		if (input == null || "".equals(input)) {
			return input;
		}
		if (input.indexOf("jinrui") > -1) {
			StringBuffer buf = new StringBuffer();
			buf.append("<span class=\"hilite\">").append(input).append("</span>");
			return buf.toString();
		}
		return input;
	}
%>
<%
    // Get parameters
    String log = request.getParameter("log");
    String numLinesParam = request.getParameter("lines");
    String mode = request.getParameter("mode");
    String day = request.getParameter("day");

	int numLines = 50;
	
    try {numLines = Integer.parseInt(numLinesParam);}catch (Exception ignored) {}
    
    // Only allow requests for valid log file names.
    if (!("debug".equals(log) || "warn".equals(log) || "info".equals(log) || "error".equals(log))) {
        log = null;
    }

    // Set defaults
    if (log == null) {
        log = "error";
    }
    
    if (mode == null) {
        mode = "asc";
    }
  
    if (day == null) {
    	day = "";
    }else{
    	day = "."+day;
    }
  
    if (numLinesParam == null) {
        numLinesParam = "50";
    }

    // Other vars
    File logDir = new File(JawaGlobals.getHomeDirectory(),"logs");
    String filename = log + ".log"+day;
    File logFile = new File(logDir, filename);
    
    String lines[] = new String[0];
    int start = 0;
    try {
	    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(logFile), "GBK"));
	    String line;
	    int totalNumLines = 0;
	    while ((line=in.readLine()) != null) {
	        totalNumLines++;
	    }
	    in.close();
	    // adjust the 'numLines' var to match totalNumLines if 'all' was passed in:
	    if ("all".equals(numLinesParam)) {
	        numLines = totalNumLines;
	    }
	    lines = new String[numLines];
	    in = new BufferedReader(new InputStreamReader(new FileInputStream(logFile), "GBK"));
	    // skip lines
	    start = totalNumLines - numLines;
	    if (start < 0) { start = 0; }
	    for (int i=0; i<start; i++) {
	        in.readLine();
	    }
	    int i = 0;
	    if ("asc".equals(mode)) {
	        while ((line=in.readLine()) != null && i<numLines) {
	            //line = StringUtils.escape(line);
	            line = (line);
	            line = parseDate(line);
	            line = hilite(line);
	            lines[i] = line;
	            i++;
	        }
	    }
	    else {
	    	
	        int end = lines.length-1;
	        while ((line=in.readLine()) != null && i<numLines) {
	            line =(line);
	            line = parseDate(line);
	            line = hilite(line);
	            lines[end-i] = line;
	            i++;
	        }
    }
	    numLines = start + i;
    } catch (FileNotFoundException ex) {
    	//Log.info("Could not open (log)file.", ex);
    }
%>

<html>
	<head>
		<title><%= log %></title>
		<link rel="stylesheet" type="text/css" href="${ctx}/styles/global.css" />
		<script type="text/javascript" src="${ctx}/scripts/jquery.js"></script>
		<script type="text/javascript" src="${ctx}/scripts/global.js"></script>
		<style type="text/css">
.log {
	overflow:auto;
	padding: .0 1em ;
}

.log table {
	border: 1px #ccc solid;
	width:auto;
}

.log tr th {
	font-size: 90%;
	text-align: center;
	background-color: #ddd;
	border-bottom: 1px #ccc solid;
	padding-left: 2px;
	padding-right: 2px;
}

.log td {
	font-size: 90%;
	background-color: #ffe;
}


.log .num {
	width: 1%;
	background-color: #eee !important;
	padding-left: 2px;
	padding-right: 2px;
}

.log .line {
	padding-left: .5em;
}

.log .date {
	font-weight: bold;
	color: #000;
}

.hilite {
	font-weight: bold;
	background-color: #ff0;
	color: #000;
}
</style>
	</head>
	<body>
		<div class="log">
			<table cellpadding="1" cellspacing="0" border="0" width="100%">
				<tr>
					<th class="head-num">
						行
					</th>
					<th>
						<%
					    if (!("debug".equals(log) || "warn".equals(log) || "info".equals(log) || "error".equals(log))) {
					        log = null;
					    }
						%>
					</th>
				</tr>
				<tr>
					<td width="1%" nowrap class="num">
						<%  if ("asc".equals(mode)) { %>
						<%  for (int j=start+1; j<=numLines; j++) { %>
						<%= j %><br>
						<%  } %>
						<%  } else { %>
						<%  for(int j=numLines; j>=start+1; j--) { %>
						<%= j %><br>
						<%  } %>
						<%  } %>
					</td>
					<td width="99%" class="line">
						<% for (String line1 : lines) {
           					 if (line1 != null) {
        				%>
						<nobr><%= line1 %></nobr>
						<br>
						<%    }
        					}
        				%>
					</td>
				</tr>
			</table>
		</div>

	</body>
</html>